import { reg } from 'services/home';
import * as SealService from 'services/sealService';
import { Toast } from 'antd-mobile';
import { verificationMode } from '../../../utils/Enum'
export default {
  namespace: 'signatureModel',
  state: {
    list: {
      productList: '',
      bannerList: '',
      Session: ''
    },
    formsList: [],
    query: {},
    isShowSelect: false,  //判断是否显示下方选择认证方式
    verificationMode: verificationMode.SMSVERIFICATION  //默认选择短信验证
  },

  effects: {
    *getOperateInfo({ payload, callback }, { call, put }) {
      const response = yield call(SealService.getOperateInfo, payload);
      if (response) {
        callback(response)
      }
      else {
        Toast.fail('获取操作权限失败！')
      }

    },

    *submitConfirm({ payload, callback }, { call, put }) {
      const response = yield call(SealService.postSubmitConfirm, payload);
      if (response.code.substring(response.code.length - 4) === '0000') {
        callback(1)
        // Toast.success('确认签署成功！');
      } else {
        Toast.fail('确认签署失败！');
        callback(0);
      }
    },

    *editFormsData({ payload, callback }, { call, put, select }) {

      const { formsList } = yield select((_) => _.signatureModel);
      let responseUpdate = {};
      let responseDelete = {};
      let responseAdd = {};
      if (payload.news.length !== 0) {
        responseAdd = yield call(SealService.postFormsAdd, payload);
        if (responseAdd.code.substring(responseAdd.code.length - 4) !== '0000') {
          callback(2);
          return
        }
      }
      if (payload.updates.length !== 0) {
        responseUpdate = yield call(SealService.putFormsUpdate, payload);
        if (responseUpdate.code.substring(responseUpdate.code.length - 4) !== '0000') {
          callback(2);
          return
        }
      }
      if (payload.deletes.forms.length !== 0) {
        responseDelete = yield call(SealService.delFormsDelete, payload);
        if (responseDelete.statusCode === 204) {
          yield callback(1)
        }
        else {
          callback(2)
        }
      } else {
        yield callback(1)
      }
      if (payload.deletes.forms.length === 0 && payload.updates.length === 0 && payload.updates.length === 0) {
        if (formsList.length !== 0)//查看是否有已添加的表单
        {
          callback(1);
        }
        else {
          callback(0);
          // Toast.fail('还未添加任何表单！')
        }
      }
    },

    *postFormsAdd({ payload, callback }, { call, put }) {
      const response = yield call(SealService.postFormsAdd, payload);
      if (response.code.substring(response.code.length - 4) === '0000') {
        callback(1)
      } else {
        Toast.fail('添加表单失败！');
      }
    },

    *getFormsSearch({ payload, callback }, { call, put }) {
      const response = yield call(SealService.getFormsSearch, payload);
      if (response.code.substring(response.code.length - 4) === '0000') {
        yield put({
          type: 'saveForms',
          payload: response.forms
        });
        callback(response.forms);
        // Toast.success('查询表单成功!')
      } else {
        Toast.fail('查询表单失败!')
      }
    },

    *putFormsUpdate({ payload }, { call, put }) {
      const response = yield call(SealService.putFormsUpdate, payload);
      if (response.code.substring(response.code.length - 4) === '0000') {
        // Toast.success('更新表单项成功!')
      } else {
        Toast.fail('更新表单项失败');
      }
    },

    *delFormsDelete({ payload }, { call, put }) {
      const response = yield call(SealService.delFormsDelete, payload);
      if (response.statusCode === '204') {
        // Toast.success('删除表单成功！');
      } else {
        Toast.fail('删除表单失败！');
      }
    },
  },


  reducers: {
    setData(state, { payload }) {
      return {
        ...state,
        list: payload,
      }
    },
    saveForms(state, { payload }) {
      return {
        ...state,
        formsList: payload,
      }
    },
    changeSession(state, { payload }) {
      return {
        ...state,
        query: payload
      }
    },
    setMask(state, { payload }) {
      return {
        ...state,
        ...payload
      }
    }
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(async ({ pathname, search, query }) => {
        if (pathname == '/Signature' || pathname == '/') {
          await dispatch({
            type: 'changeSession',
            payload: query
          })
        }
      });
    },
  },
};
